/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function UploadFile(filePath){
	this.jsjava_class="jsorg.eob.document.UploadFile";
	this.filePath=filePath;
	this.imageTypes=new HashSet();
	this.imageTypes.add("gif");
	this.imageTypes.add("jpg");
	this.imageTypes.add("bmp");
	if(filePath){
		var dotPos=filePath.lastIndexOf(".");
		if(dotPos!=-1){
			this.suffix=filePath.substring(dotPos+1);
		}	
	}
	this.isImageLoaded=false;
	if(filePath){
		this.loadDatum();
	}
}

/**
 * Set the url of the file to be upload
 * @param filePath
 */
UploadFile.prototype.setFile=function(filePath){
	this.filePath=filePath;
	var dotPos=filePath.lastIndexOf(".");
	if(dotPos!=-1){
		this.suffix=filePath.substring(dotPos+1);
	}
};

/**
 * Load file data
 */
UploadFile.prototype.loadDatum=function(){	
	var ufo=this;		
	function getDatum(){
		if(!ufo.filePath){
			return [-1,-1,-1];
		}
		var arr=new Array(3);
		if(BrowserUtils.isIE()&&ufo.isImage()){	
			var img=new Image;
			img.src=ufo.filePath;		
			if(img.readyState!="complete"){
				arr[0]=-1;
				arr[1]=-1;
				arr[2]=-1;
				return arr;
			}
			arr[0]=img.fileSize;
			arr[1]=img.width;
			arr[2]=img.height;
			this.isImageLoaded=true;
			return arr;
		}else{
			if(BrowserUtils.isIE()){
				var fos=new ActiveXObject("Scripting.FileSystemObject");
				var file=fos.GetFile(ufo.filePath);
				arr[0]=file.Size;
				return arr;
			}else{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
				var file=new java.io.File(ufo.filePath);
				arr[0]=file.length();
				arr[1]=-1;
				arr[2]=-1;
				return arr;
			}
		}		
	}
	var datum=getDatum();
	this.fileSize=datum[0];
	this.imageWidth=datum[1];
	this.imageHeight=datum[2];
};

/**
 * Judge whether the file is image
 */
UploadFile.prototype.isImage=function(){
	return this.imageTypes.contains(this.suffix);
};

/**
 * Add new image type
 * @param imageType
 */
UploadFile.prototype.addImageType=function(imageType){
	this.imageTypes.add(imageType);
};

/**
 * Add image types
 * @param imageTypes
 */
UploadFile.prototype.addImageTypes=function(imageTypes){
	for(var i=0;i<imageTypes.length;i++){
		this.imageTypes.add(imageTypes[i]);
	}
};

/**
 * Get the file size
 */
UploadFile.prototype.getFileSize=function(){
	this.loadDatum();
	return this.fileSize;
};

/**
 * Get the the width of image file
 */
UploadFile.prototype.getImageWidth=function(){
	this.loadDatum();
	return this.imageWidth;
};

/**
 * Get the the height of image file
 */
UploadFile.prototype.getImageHeight=function(){
	this.loadDatum();
	return this.imageHeight;
};